package com.lehecai.core.lottery.fetcher.jcgyj.impl.worker;

import com.lehecai.core.exception.FetchFailedException;
import com.lehecai.core.exception.UnsupportedFetcherTypeException;
import com.lehecai.core.lottery.JcgyjRaceStatus;
import com.lehecai.core.lottery.fetcher.FetcherType;
import com.lehecai.core.lottery.fetcher.jcgyj.AbstractJcgjScheduleFetchWorker;
import com.lehecai.core.lottery.fetcher.jcgyj.IJcgjScheduleFetcher;
import com.lehecai.core.lottery.fetcher.jcgyj.JcgjScheduleItem;
import com.lehecai.core.lottery.fetcher.jcgyj.impl.CommonJcgjScheduleFetcher;
import com.lehecai.core.util.CharsetConstant;
import com.lehecai.core.util.CoreHttpUtils;
import com.lehecai.core.util.CoreJSONUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用竞彩足球猜冠军赛程抓取实现类
 * @author qatang
 *
 */
public class CommonJcgjScheduleFetchWorkerPengineAPI extends AbstractJcgjScheduleFetchWorker {
	protected final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	
	private static final String PROCESS_CODE = "10001"; 
	private static final String PROCESS_TYPE = "jcgj";
	
	private static final String URL_PENGINE_API = "http://phase.lehecai.com:16041/JcServlet";
	//private static final String URL_PENGINE_API = "http://172.16.8.71:8080/devpengine/JcServlet";
	
	@SuppressWarnings("unchecked")
	@Override
	public List<JcgjScheduleItem> fetch(String phase)
			throws FetchFailedException {
		List<String> list = null;
		try {
			list = CoreHttpUtils.getUrl(URL_PENGINE_API, "ProcessCode=" + PROCESS_CODE + "&phase=" + phase + "&type=" + PROCESS_TYPE, CharsetConstant.CHARSET_UTF8, 100000);
		} catch (Exception e) {
			logger.error("竞彩猜冠军抓取官方赛程失败", e);
			throw new FetchFailedException(e.getMessage());
		}
		if (list == null || list.size() == 0) {
			logger.error("未获取到竞彩猜冠军官方赛程");
			throw new FetchFailedException("未获取到竞彩足球猜冠军官方赛程");
		}
		String matches = list.get(0);
		if (matches == null || "".equals(matches)) {
			logger.error("未获取到竞彩足球猜冠军官方赛程");
			throw new FetchFailedException("未获取到竞彩足球猜冠军官方赛程");
		}
		JSONArray jsonArray = null;
		try {
			jsonArray = JSONArray.fromObject(matches);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		
		if (jsonArray == null) {
			logger.error("转换竞彩猜冠军官方赛程出错");
			throw new FetchFailedException("转换竞彩猜冠军官方赛程出错");
		}
		if (jsonArray.isEmpty()) {
			logger.warn("竞彩猜冠军官方赛程为空");
			return null;
		}
		List<JcgjScheduleItem> jcgjScheduleItems = new ArrayList<JcgjScheduleItem>();
        for (Object aJsonArray : jsonArray) {
            JSONObject object = (JSONObject) aJsonArray;
            if (object == null) {
                throw new FetchFailedException("转换竞彩猜冠军官方赛程出错");
            }
            JcgjScheduleItem item = new JcgjScheduleItem();

            item.setMatchNum(CoreJSONUtils.getInt(object, "matchNum") + "");
            item.setOfficialNum(CoreJSONUtils.getString(object, "officialNum"));
            item.setPhase(phase);
            item.setStatus(JcgyjRaceStatus.getItem(CoreJSONUtils.getInt(object, "status")));
            item.setTeam(CoreJSONUtils.getString(object, "teamName"));
            jcgjScheduleItems.add(item);
        }
		return jcgjScheduleItems;
	}
	public static void main(String[] args) {
		IJcgjScheduleFetcher fetcher = new CommonJcgjScheduleFetcher();
		List<JcgjScheduleItem> jcgjScheduleItemList = null;
		try {
			jcgjScheduleItemList = fetcher.fetch("2014001", FetcherType.T_PENGINEAPI);
		} catch (UnsupportedFetcherTypeException e) {
			e.printStackTrace();
		} catch (FetchFailedException e) {
			e.printStackTrace();
		}
		for (JcgjScheduleItem item : jcgjScheduleItemList) {
			System.out.println(item.getJczqScheduleInfo());
		}
	}
}
